package com.etc.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.etc.entity.User;
import com.etc.util.PageData;
import com.etc.util.ResponseData;
import com.etc.zmall.service.UserService;
import com.etc.zmall.service.impl.UserServiceImpl;
import com.google.gson.Gson;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/user.do")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	UserService service = new UserServiceImpl();

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 先写查询
		// op = query 模糊查询 + 分页
		String op = "query";

		if (request.getParameter("op") != null) {
			op = request.getParameter("op");
		}

		if ("query".equals(op)) {
			doQuery(request, response);
		}else if("blogin".equals(op)) {
			doLogin(request, response);
		}

	}
	protected void doLogin(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		//1. 验证码
		//接收用户提交的验证码
		String requestVcode = request.getParameter("vcode");
		
		//获取servletContext中的code
		String code = (String) request.getServletContext().getAttribute("code");
		
		//比较
		if(requestVcode.equals(code)) {
			//相等
			System.out.println("验证码输入正确");
			////2. 用户名和密码
			
		}
		else {
			//响应结果，提示用户验证码出错
			System.out.println("验证码输入错误");
		}
		
		
		
		
	}

	/**
	 * @查询方法
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	protected void doQuery(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("application/json;charset=utf-8");
		PrintWriter pw = response.getWriter();
		
		
		int pageNo = 1;
		if (request.getParameter("page") != null) {
			pageNo = Integer.parseInt(request.getParameter("page"));
		}

		int pageSize = 10;
		if (request.getParameter("limit") != null) {
			pageSize = Integer.parseInt(request.getParameter("limit"));
		}

		String keywords = "";
		if (request.getParameter("keywords") != null) {
			keywords = request.getParameter("keywords");
		}
		
		String usersex = "";
		if (request.getParameter("usersex") != null) {
			usersex = request.getParameter("usersex");
		}

		// 调用service
		PageData<User> pd = service.getUserByPage(pageNo, pageSize, keywords,usersex);

		
		ResponseData<User> rdata = new ResponseData<User>();
		rdata.setCount(pd.getTotalCount());
		rdata.setData(pd.getList());
		
		// 返回json数据
		Gson gson = new Gson();
		String jsonresult = gson.toJson(rdata);
		
		pw.print(jsonresult);
		
		pw.close();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
